package org.apache.i18n.translater;

import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.Map.Entry;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class PowerTranslater {
	
	static private Log log= LogFactory.getLog("Translater");
	Translater translater;
	public PowerTranslater(Translater translater)
	{
		this.translater=translater;
	}

	/**
	 * 属性文件
	 * @param prop
	 * @return
	 */
	public  Properties doProperties(Properties prop){
		Properties result=new Properties();
		
		for (Entry<Object, Object> entry : prop.entrySet()) {
			String from=(String)entry.getValue();
			String to=translater.translateSentence(from);
			
			log.info("from:"+ from+" to:"+to);
			result.put(entry.getKey(), to);
		}
		return result;
		
	}
	
	

	public  void doPropertiesFile(File fromFile,File toFile,String comments)
	{		
		Properties from=new Properties();

	    try {
			from.load(new FileInputStream(fromFile));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    

	    Properties to=this.doProperties(from);  

	    
		try {
			to.store(new FileOutputStream(toFile), comments);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    
		log.info("---------------------------------");
		log.info(fromFile.toString()+" line:"+to.size());
		log.info("---------------------------------");
	}
	
	

	public  void translatePropertiesDir(File fromDir,File toDir,
			FileFilter filter,String suffix,String comments)
	{		
		//处理本个目录下的内容
		File files[]=fromDir.listFiles(filter);
		for (int i = 0; i < files.length; i++) {
			File file = files[i];
			if(file.isDirectory())
			{
				translatePropertiesDir(file,new File(toDir,"dirName"),filter,suffix,comments);
			}else{
				log.info(file);
			}
		}
		
		//
	}
}
